local skel = fk.CreateSkill {
  name = "emo__huanwu",
}

Fk:loadTranslationTable{
  ["emo__huanwu"] = "唤雾",
  [":emo__huanwu"] = "出牌阶段，你可以将♠牌当【雷电】对一名角色使用，立即生效三次，每次伤害递减1点，最后置入弃牌堆。",
  ["#emo__huanwu"] = "唤雾：将♠牌当【雷电】对一名角色使用，生效3次",
}

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = "#emo__huanwu",
  card_num = 1,
  target_num = 1,
  can_use = Util.TrueFunc,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and Fk:getCardById(to_select).suit == Card.Spade then
      local card = Fk:cloneCard("lightning")
      card:addSubcard(to_select)
      card.skillName = skel.name
      return not player:prohibitUse(card)
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected == 0 and #selected_cards == 1 then
      local card = Fk:cloneCard("lightning")
      card:addSubcard(selected_cards[1])
      card.skillName = skel.name
      return not player:isProhibited(to_select, card)
    end
  end,
  on_use = function(self, room, use)
    local player = use.from
    local to = use.tos[1]
    local c = Fk:cloneCard("lightning")
    c:addSubcards(use.cards)
    c.skillName = skel.name
    room:useCard{
      from = use.from,
      tos = {to},
      card = c,
    }
    local cid = use.cards[1]
    if table.contains(to:getCardIds("j"), cid) then
      local card = to:removeVirtualEquip(cid)
      if not card or card.name ~= "lightning" then return end
      room:moveCardTo(card, Card.Processing, nil, fk.ReasonPut, skel.name)
      for i = 1, 3 do
        if to.dead then break end
        local effect_data = CardEffectData:new {
          card = card,
          to = to,
          tos = { to },
          extra_data = { emo__huanwu_minus = i - 1 }
        }
        room:doCardEffect(effect_data)
      end
      if room:getCardArea(cid) == Card.PlayerJudge then
        room:moveCardTo(cid, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile)
      end
    end
  end,
})

skel:addEffect(fk.PreDamage, {
  can_refresh = function (self, event, target, player, data)
    return player.seat == 1 and data.card
  end,
  on_refresh = function (self, event, target, player, data)
    local EffectEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
    if EffectEvent then
      local effect = EffectEvent.data
      if effect.card == data.card and effect.extra_data and effect.extra_data.emo__huanwu_minus then
        local minus = effect.extra_data.emo__huanwu_minus
        if minus > 0 then
          data:changeDamage(-minus)
        end
      end
    end
  end,
})

return skel
